/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.front.wallet;

import freemarker.template.utility.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.account.AccountService;
import shop.services.front.account.bean.Account;
import shop.services.front.organize.OrganizeService;
import shop.services.front.organize.bean.Organize;
import shop.services.front.record.RecordService;
import shop.services.front.record.bean.Record;
import shop.services.front.wallet.WalletService;
import shop.services.front.wallet.bean.Wallet;
import shop.services.manage.system.bean.User;
import shop.services.manage.system.impl.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.List;

/**   
 * @类名称：WalletController
 * @创建人：Ltz   
 * @创建时间：2017-08-12 上午10:15:30      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/front/wallet")
public class WalletController extends FrontBaseController<Wallet> {
    @Resource
    private WalletService walletService;
    @Resource
    private AccountService accountService;
    @Resource
    private UserService userService;
    @Resource
    private RecordService recordService;
    @Resource
    private OrganizeService organizeService;
    @Override
    public Services<Wallet> getService() {
        return walletService;
    }

    /**
     * 跳转到交易记录页面
     */
    @RequestMapping("/toRecordList")
    public String toRecordList() {
        return "/front/wallet/recordList";
    }

    /**
     * 跳转到提现方式页面
     */
    @RequestMapping("/toChooseList")
    public String toChooseList() {
        return "/front/wallet/chooseList";
    }

    /**
     * 跳转到申请提现页面
     */
    @RequestMapping("/toAddWallet")
    public String toAddWallet() {
        return "/front/wallet/addWallet";
    }


    /**
     * 提现申请
     *
     * @return jsonResult
     * @throws Exception
     */
    @RequestMapping(value = "/insertWallet", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertWallet(HttpServletRequest request, @ModelAttribute("wallet") Wallet wallet) throws Exception {
        Account account = (Account) request.getSession().getAttribute("accountInfo");
        User user = (User) request.getSession().getAttribute("userInfo");
        jsonResult = new JSONResult();
        DecimalFormat df = new DecimalFormat("######0.00");
        if(account!=null){//判断用户类型
            if(Double.parseDouble(account.getUnCash())>=Double.parseDouble(wallet.getMoney())){
                account.setCash(String.valueOf(df.format(Double.parseDouble(account.getCash()) + Double.parseDouble(wallet.getMoney()))));//计算已提现
                account.setUnCash(String.valueOf(df.format(Double.parseDouble(account.getUnCash()) - Double.parseDouble(wallet.getMoney()))));//计算未提现
                wallet.setApplicantName(account.getName());
                wallet.setApplicant(account.getId());
                wallet.setApplicantType("1");
                if(!StringUtils.isBlank(account.getOrganizeID())){//志愿者所属机构
                    Organize organize =new Organize();
                    organize.setId(account.getOrganizeID());
                    organize=organizeService.selectOne(organize);
                    wallet.setOrganizeName(organize.getName());
                }
                int rs = getService().insert(wallet);//插入提现申请表
                accountService.update(account);//更新用户信息
                request.getSession().setAttribute("accountInfo", account);//更新缓存
                Record record =new Record();
                record.setUserID(account.getId());
                record.setUserType("1");
                record.setTransType(wallet.getAccountType());
                record.setMoney(wallet.getMoney());
                record.setStatus("1");
                record.setwID(String.valueOf(rs));
                recordService.insert(record);//插入交易记录表
                return jsonResult;
            }else{
                jsonResult.setMessage("您的余额不足，无法进行提现！");
                jsonResult.setSuccess(false);
                return jsonResult;
            }
        }else if(user !=null){//判断用户类型
            if(Double.parseDouble(user.getUnCash())>=Double.parseDouble(wallet.getMoney())){
                user.setCash(String.valueOf(df.format(Double.parseDouble(user.getCash()) + Double.parseDouble(wallet.getMoney()))));//计算已提现
                user.setUnCash(String.valueOf(df.format(Double.parseDouble(user.getUnCash()) - Double.parseDouble(wallet.getMoney()))));//计算未提现
                wallet.setOrganizeName(user.getNickname());
                wallet.setApplicantType("2");
                wallet.setApplicant(user.getId());
                wallet.setApplicantName(user.getNickname());
                int rs = getService().insert(wallet);//插入提现申请表
                userService.update(user);//更新用户信息
                request.getSession().setAttribute("userInfo", user);//更新缓存
                Record record =new Record();
                record.setUserID(user.getId());
                record.setUserType("2");
                record.setTransType(wallet.getAccountType());
                record.setMoney(wallet.getMoney());
                record.setStatus("1");
                record.setwID(String.valueOf(rs));
                recordService.insert(record);//插入交易记录表
                return jsonResult;
            }else{
                jsonResult.setMessage("您的余额不足，无法进行提现！");
                jsonResult.setSuccess(false);
                return jsonResult;
            }
        }else{
            jsonResult.setSuccess(false);
            jsonResult.setMessage("您还未登录，无法进行提现！");
            return jsonResult;
        }
    }
}
